package com.chijun.mform.modify;


import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.chijun.mform.TheResultsListener;
import com.chijun.mform.create.IWaitResult;
import com.chijun.mform.create.field.Field;
import com.chijun.mform.create.mode.FormGroup;
import com.chijun.mform.create.mode.FormIcon;
import com.chijun.mform.test.CreateFormServiceImpl;
import com.example.ccj.b.c.dialog.ErrorDialog;
import com.example.ccj.u.JsonUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.server.MyObserver;
import com.example.ccj.u.server.Result;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 表单创建初始配置
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2025/2/5 16:55
 */
public class FormBaseConfig {

    public static final int TYPE_LOCAL_FORM = 0;
    public static final int TYPE_LOCAL_NETWORK = 1;

    //表单类型，本地或者网络的
    public int formType = TYPE_LOCAL_FORM;                                 //表单类型，默认是本地表单

    public Field[] fields;                                                 //可使用的字段列表
    public List<FormGroup> formGroupList;                                  //表单分组列表
    public List<FormIcon> formIconList;                                    //表单图标列表
    public List<String> timeType;                                          //时间格式列表
    public IModifyFormListener modifyFormListener;


    public FormBaseConfig(){

        this.modifyFormListener = new IModifyFormListener() {
            //获取表单配置
            @Override
            public void getCreateFormConfig(IWaitResult iWaitResult) {
                CreateFormServiceImpl.getInstance().getCreateFormData()
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new MyObserver<Result>() {
                            @Override
                            public void onNext(Result result) {

                                JSONObject jsonObject = JSONObject.parseObject(result.getData());
                                //字段配置
                                if (jsonObject.containsKey("field")) {
                                    String fieldStr = jsonObject.getString("field");
                                    if (!StringUtils.isEmpty(fieldStr)) {
                                        List<Field> listBean = JsonUtils.toListBean(fieldStr, Field.class);
                                        if (listBean != null) {
                                            Field[] fields = new Field[listBean.size()];
                                            for (int i = 0; i < listBean.size(); i++) {
                                                fields[i] = listBean.get(i);
                                            }
//                                            formBaseConfig.fields = fields;
                                        } else {
                                            LogUtils.w("未获取到字段配置！");
                                        }
                                    }
                                }

                                //表单类型配置
                                if (jsonObject.containsKey("group")) {
                                    String fieldStr = jsonObject.getString("group");
                                    if (!StringUtils.isEmpty(fieldStr)) {
//                                        formBaseConfig.formGroupList = JsonUtils.toListBean(fieldStr, FormGroup.class);
                                    } else {
                                        LogUtils.w("未获取到表单分组配置！");
                                    }
                                }

                                //日期格式配置
                                if (jsonObject.containsKey("time")) {
                                    String fieldStr = jsonObject.getString("time");
                                    if (!StringUtils.isEmpty(fieldStr)) {
//                                        formBaseConfig.timeType = JsonUtils.toListBean(fieldStr, String.class);
                                    }
                                }



                                iWaitResult.onResult(result.getData());
                            }
                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                iWaitResult.onError(e.getMessage());
                            }
                        });
            }

            @Override
            public void save(Context context, String formSaveData, TheResultsListener resultsListener) {

            }

            @Override
            public void resultTreeModeList(IWaitResult iWaitResult) {

            }
        };
    }


}
